<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Excel 转换工具</title>
    <script src="./js/xlsx.full.min.js"></script>
    <script src="./js/uni.webview.1.5.6.js"></script>
    <script src="./js/FileSaver.min.js"></script>
    <style>
        div,button{
            -webkit-tap-highlight-color:transparent
        }
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            padding: 20px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            /* min-height: calc(100vh - 44px); */
            min-height: 100vh;
            margin: 0;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            background: rgba(255, 255, 255, 0.95);
            border-radius: 20px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
            padding: 40px;
            text-align: center;
        }
        h2 {
            color: #333;
            margin-bottom: 30px;
            font-weight: 600;
        }
        .file-upload-wrapper {
            position: relative;
            width: 100%;
            height: 200px;
            border: 3px dashed #667eea;
            border-radius: 15px;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            background: rgba(102, 126, 234, 0.05);
            transition: all 0.3s ease;
            cursor: pointer;
            overflow: hidden;
        }
        /* 移除hover效果，添加触摸优化 */
        .file-upload-wrapper.active, .file-upload-wrapper.dragover {
            background: rgba(102, 126, 234, 0.1);
            border-color: #764ba2;
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
        }
        .file-upload-wrapper::before {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: linear-gradient(45deg, #667eea, #764ba2);
            opacity: 0.1;
            z-index: -1;
        }
        .upload-icon {
            font-size: 60px;
            color: #667eea;
            margin-bottom: 15px;
            transition: all 0.3s ease;
        }
        .file-upload-wrapper.active .upload-icon {
            transform: scale(1.1);
            color: #764ba2;
        }
        .file-upload-text {
            font-size: 18px;
            color: #555;
            margin-bottom: 10px;
        }
        .file-upload-hint {
            font-size: 14px;
            color: #888;
        }
        .file-input {
            display: none;
        }
        .file-info {
            margin-top: 20px;
            padding: 15px;
            background: #f8f9fa;
            border-radius: 10px;
            display: none;
        }
        .file-info.show {
            display: block;
            animation: fadeIn 0.5s ease;
        }
        .file-name {
            font-weight: 600;
            color: #333;
        }
        .file-size {
            color: #666;
            font-size: 14px;
        }
        .confirm-button {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            padding: 12px 30px;
            font-size: 16px;
            border-radius: 50px;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
            margin-top: 20px;
            display: none;
        }
        /* 添加active状态替代hover */
        .confirm-button:active {
            transform: translateY(-3px);
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
        }
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(10px); }
            to { opacity: 1; transform: translateY(0); }
        }
        .pulse {
            animation: pulse 2s infinite;
        }
        @keyframes pulse {
            0% { box-shadow: 0 0 0 0 rgba(102, 126, 234, 0.4); }
            70% { box-shadow: 0 0 0 15px rgba(102, 126, 234, 0); }
            100% { box-shadow: 0 0 0 0 rgba(102, 126, 234, 0); }
        }
    </style>
</head>
<body>
    <div class="container">
      <h2>Excel 文件转换工具</h2>
      <div class="file-upload-wrapper pulse drop-zone" id="dropZone">
        <div class="upload-icon">📁</div>
        <div class="file-upload-text">拖放文件到这里或点击选择</div>
        <div class="file-upload-hint">支持 .xlsx, .xls 格式</div>
        <input type="file" class="file-input" accept=".xlsx,.xls,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel" />
      </div>
      
      <div class="file-info">
        <div>已选择文件:</div>
        <div class="file-name"></div>
        <div class="file-size"></div>
      </div>
      
      <button class="confirm-button">确定</button>
    </div> 

    <script>
        const dropZone = document.querySelector('.drop-zone');
        const fileInput = document.querySelector('.file-input');
        const fileInfo = document.querySelector('.file-info');
        const fileName = document.querySelector('.file-name');
        const fileSize = document.querySelector('.file-size');
        const confirmButton = document.querySelector('.confirm-button');
        // 文件
        let selectedFile = null;

        // 点击选择文件
        dropZone.addEventListener('click', (e) => {
            // 阻止事件冒泡，避免触发两次
            if (e.target !== fileInput) {
                // 添加active状态以提供视觉反馈
                dropZone.classList.add('active');
                setTimeout(() => {
                    dropZone.classList.remove('active');
                }, 300);
                fileInput.click();
            }
        });

        // 文件选择处理
        fileInput.addEventListener('change', function(e) {
            if (this.files && this.files[0]) {
                selectedFile = this.files[0];
                showFileInfo(selectedFile);
            }
        });

        // 拖拽事件处理
        ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
            dropZone.addEventListener(eventName, preventDefaults, false);
        });

        function preventDefaults(e) {
            e.preventDefault();
            e.stopPropagation();
        }

        ['dragenter', 'dragover'].forEach(eventName => {
            dropZone.addEventListener(eventName, highlight, false);
        });

        ['dragleave', 'drop'].forEach(eventName => {
            dropZone.addEventListener(eventName, unhighlight, false);
        });

        function highlight() {
            dropZone.classList.add('dragover');
        }

        function unhighlight() {
            dropZone.classList.remove('dragover');
        }

        // 处理文件拖放
        dropZone.addEventListener('drop', handleDrop, false);

        function handleDrop(e) {
            const dt = e.dataTransfer;
            const files = dt.files;
            
            if (files.length) {
                // 检查文件类型
                const file = files[0];
                if (file.name.endsWith('.xlsx') || file.name.endsWith('.xls')) {
                    selectedFile = file;
                    showFileInfo(file);
                } else {
                    alert('请选择 Excel 文件 (.xlsx 或 .xls)');
                }
            }
        }

        // 显示文件信息
        function showFileInfo(file) {
            fileName.textContent = file.name;
            fileSize.textContent = formatFileSize(file.size);
            fileInfo.classList.add('show');
            confirmButton.style.display = 'inline-block';
            dropZone.classList.remove('pulse');
        }

        // 格式化文件大小
        function formatFileSize(bytes) {
            if (bytes === 0) return '0 Bytes';
            const k = 1024;
            const sizes = ['Bytes', 'KB', 'MB', 'GB'];
            const i = Math.floor(Math.log(bytes) / Math.log(k));
            return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
        }

        // 确定按钮处理
        confirmButton.addEventListener('click', function() {
            if (!selectedFile) return;
         
            const reader = new FileReader();
            reader.onload = function(e) {
                const data = new Uint8Array(e.target.result);
                const workbook = XLSX.read(data, { type: 'array' });
                
                // 获取第一个工作表
                const firstSheetName = workbook.SheetNames[0];
                const worksheet = workbook.Sheets[firstSheetName];
                
                // 转换为 JSON
                const jsonData = XLSX.utils.sheet_to_json(worksheet);
                
                // 显示结果（仅用于调试）
                // document.getElementById('result').textContent = JSON.stringify(jsonData, null, 2);
                // uni.navigateTo({url:"/pages/offline/offline?jsonData"})
                // 发送数据回 UniAppd
                // if (window.uni && window.uni.postMessage) {
                    // window.postMessage({
                    //     data: {
                    //         type: 'excelData',
                    //         payload: jsonData
                    //     }
                    // })
                uni.postMessage({
                data: {
                    action: jsonData
                }
                });
            };
            reader.readAsArrayBuffer(selectedFile);

        })
    </script>
</body>
</html>